package com.leo.util.tcpclient;





import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.junit.Test;

public class TestClient {

	
	@Test
	public void test(){
		int numTasks = 1;

		ExecutorService exec = Executors.newCachedThreadPool();
		for (int i = 0; i < numTasks; i++) {
			exec.execute(createTask(i));
		}
	}

	// 定义一个简单的任务
	private static Runnable createTask(final int taskID) {
		
		return new Runnable() {
			private Socket socket = null;

			public void run() {
				try {
					System.out.println("Task " + taskID + ":start");
					long startTime = System.currentTimeMillis();   //获取开始时间
					//String ip = "106.3.226.140";
					String ip = "127.0.0.1";
					//String ip = "192.168.1.177";
					socket = new Socket(ip, 10091);
					//socket.setSoTimeout(5000);
					// 发送关闭命令
					System.out.println("IP ---------" + socket.getLocalSocketAddress());
					OutputStream socketOut = socket.getOutputStream();
					
					ByteBuffer byteBuffer = ByteBuffer.allocate(205);

					ByteBuffer binVer = ByteBuffer.allocate(4);
					binVer.put("v301".getBytes());

					ByteBuffer custId = ByteBuffer.allocate(32);
					custId.put("CS00010002".getBytes());

					ByteBuffer configNo = ByteBuffer.allocate(16);
					configNo.put("C13122817140047".getBytes());

					ByteBuffer fileNo = ByteBuffer.allocate(16);
					fileNo.put("C13122817140047".getBytes());

					ByteBuffer androidId = ByteBuffer.allocate(32);
					androidId.put("ddbbc57c1a2f8888".getBytes());

					ByteBuffer imsi = ByteBuffer.allocate(32);
					imsi.put("460020000000000".getBytes());

					ByteBuffer plat = ByteBuffer.allocate(16);
					plat.put("MT8888".getBytes());

					ByteBuffer defaultStr = ByteBuffer.allocate(16);
					defaultStr.put("11".getBytes());

					ByteBuffer defaultStr2 = ByteBuffer.allocate(16);
					defaultStr2.put("".getBytes());

					ByteBuffer notPross = ByteBuffer.allocate(15);
					notPross.put("".getBytes());
					
					byteBuffer.put(binVer.array());
					byteBuffer.putInt(0);
					byteBuffer.put(custId.array());
					byteBuffer.put(configNo.array());
					byteBuffer.put(fileNo.array());
					byteBuffer.put(androidId.array());
					byteBuffer.put(imsi.array());
					byteBuffer.put(plat.array());
					byteBuffer.put(defaultStr.array());
					byteBuffer.put(defaultStr2.array());
					byteBuffer.putInt(Integer.reverseBytes(17));
					byteBuffer.put(notPross.array());
					byteBuffer.rewind();
					socketOut.write(byteBuffer.array());
					socketOut.flush();
					
					///Thread.sleep(6000);
					// 接收服务器的反馈
					InputStream inputStream = socket.getInputStream();
					
					int total = 152;
					byte[] rea = new byte[total]; // 客户端请求头
					int recvCount = 0; // 已经成功读取的字节的个数
					
					// 循环读缓冲区域数据，直到读完位置
					while (recvCount < total) {
						recvCount += inputStream.read(rea, recvCount, total-recvCount);
					}
					//System.out.println(new String(rea));
					
					int baoliu = 0;
					int fileLength = 0;
					int recvLength = 0;
					int isLock = 0;
					String confNo = "";
					String netNum = "";
					String wifi = "";
					String configContext = "";
					
					ByteBuffer buffer = ByteBuffer.allocate(4);
					buffer.put(rea, 0, 4);
					buffer.rewind();
					baoliu = Integer.reverseBytes(buffer.getInt());
					buffer.flip();
					
					ByteBuffer buffer2 = ByteBuffer.allocate(4);
					buffer2.put(rea, 4, 4);
					buffer2.rewind();
					fileLength = Integer.reverseBytes(buffer2.getInt());
					buffer2.flip();
					
					ByteBuffer buffer3 = ByteBuffer.allocate(4);
					buffer3.put(rea, 8, 4);
					buffer3.rewind();
					recvLength = Integer.reverseBytes(buffer3.getInt());
					buffer2.flip();
					
					ByteBuffer buffer4 = ByteBuffer.allocate(4);
					buffer4.put(rea, 12, 4);
					buffer4.rewind();
					isLock = Integer.reverseBytes(buffer4.getInt());
					buffer4.flip();
					
					confNo = new String(rea, 16, 16).trim();
					if(null == confNo || "".equals(confNo)){
						confNo = "C00000000001";
					}
					netNum = new String(rea, 32, 4).trim();
					//netNum = new String(rea, 32, 120).trim();
					wifi = new String(rea, 36, 4).trim();
					
					if(recvLength != 0){
						byte[] byteArr = new byte[recvLength]; // 客户端请求头
						int readCount = 0; // 已经成功读取的字节的个数
						// 循环读缓冲区域数据，直到读完位置
						while (readCount < recvLength) {
							readCount += inputStream.read(byteArr, readCount, recvLength-readCount);
							System.out.println("读取：" + readCount);
						}
						configContext = new String(byteArr, 0, recvLength).trim();
						System.out.println("总共接收：" + recvLength);
						
					}
					
					System.out.println("-------baoliu:" + baoliu + ", fileLength:" + fileLength + ", recvLength:" +recvLength +"," + " isLock:" +isLock + ", confNo:" + confNo + ",netNum:" + netNum + ",wifi:" + wifi + ", configContext:"+ configContext);
					
					long endTime = System.currentTimeMillis(); //获取结束时间

					System.out.println("程序运行时间： "+(endTime-startTime)/1000f+"秒");
				} catch (IOException ioEx) {
					ioEx.printStackTrace();
					try {
						if (socket != null) {
							socket.close();
						}
					} catch (IOException ioe) {
						ioe.printStackTrace();
					}
				} catch (Exception e) {
					e.printStackTrace();
					try {
						if (socket != null) {
							socket.close();
						}
					} catch (IOException ioe) {
						ioe.printStackTrace();
					}
				} finally {
					try {
						if (socket != null) {
							socket.close();
						}
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}

		};
	}
}